import request from "../../utils/request"

// pages/video/video.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [],
    curNavId: '',
    videoList: [],
    curVideoId:'',
    videoTimeList:[],
    isRefresh:false
  },

  bindrefresherrefresh(){
    this.getVideoList(this.data.curNavId)
  },
  goSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!wx.getStorageSync('userInfo')) {
      wx.redirectTo({
        url: '/pages/login/login',
      })
      return
    }
    this.getNavList()


  },
  tapNav(event) {
    let curNavId = event.target.id
    this.setData({
      curNavId,
      videoList: [],
 
    })
    this.getVideoList(curNavId)
  },
  async getNavList() {
    let res = await request('/video/group/list')
    // console.log(res,1);
    let navId = res.data[0].id
    this.setData({
      navList: res.data.slice(0, 20),
      curNavId: navId
    })
    this.getVideoList(navId)
  },
  bindplay(e) {
    let curVideoId = e.target.id
    // if (this.data.curVideoId && this.data.curVideoId != curVideoId && this.VideoContext) {
    //   this.VideoContext.pause()
    // }
    // this.id = id;
    this.setData({
      curVideoId
    })
    
    this.VideoContext = wx.createVideoContext(curVideoId)
    let existCurVideoInVideoTimeList = this.data.videoTimeList.find(item=>item.vid==curVideoId)
    existCurVideoInVideoTimeList&&this.VideoContext.seek(existCurVideoInVideoTimeList.currentTime)
    this.VideoContext.play()
  },
  bindtimeupdate(event){
    // console.log(event.detail);
    let curVideoInfo = {
      currentTime:event.detail.currentTime,
      vid:event.target.id
    }
    let videoTimeList = this.data.videoTimeList
    let existCurVideo = videoTimeList.find(item=>item.vid==curVideoInfo.vid)
    if(existCurVideo){
      existCurVideo.currentTime = curVideoInfo.currentTime
    }else{
      videoTimeList.push(curVideoInfo)
    }
    this.setData({
      videoTimeList
    })

  },
  bindended(e){
    let vid=e.target.id
    let videoTimeList = this.data.videoTimeList
    let index = videoTimeList.findIndex(item=>item.vid==vid)
    videoTimeList.splice(index,1)
    this.setData({
      videoTimeList
    })

  },
  async getVideoList(id) {
    wx.showLoading({
      title: '加载中',
    })
    let res = await request('/video/group', {
      id
    })
    // console.log(res);
    wx.hideLoading()
    let index = 0
    this.setData({
      isRefresh:false,
      videoList: res.datas.map(item => ({
        ...item,
        id: index++
      }))
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  bindscrolltolower(){
   let {videoList} = this.data
   videoList.push(...videoList)
   this.setData({
     videoList
   })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})